package com.webcamtracker.ui;

import com.webcamtracker.video.VideoSuscriptor;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;


public class FramesPerSecond extends JPanel implements VideoSuscriptor {
    private long previous;
    private JLabel fps = new JLabel(" FPS : ");
    private int frameCounter = 0;
    private static final int SECOND = 1000;

    public FramesPerSecond() {
        setLayout(new BorderLayout());
        add(fps, BorderLayout.WEST);
        this.previous = 0;
    }

    public void arrivedFrame(BufferedImage image) {
        frameCounter++;
        long now = System.currentTimeMillis();
        long delta = (now - previous);

        if (delta >= SECOND) {
            float fpsValue = frameCounter * ((float) SECOND / delta);
            fps.setText(" FPS : " + fpsValue);
            previous = now;
            frameCounter = 0;
        }
    }
}
